package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.FDDao;
import com.aaa.javaweb.entity.TuiFang;
import com.aaa.javaweb.entity.XuFang;
import com.aaa.javaweb.until.BaseDB;
import com.aaa.javaweb.until.JDBCUntil;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Array;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Repository
public class FDDaoImpl implements FDDao {
    @Override
    public List<Map> allroom() {
        /**
         * 获取所有当前房间,如果当前房间的状态为已入住,查出详情里面当前的入住信息
         */
        String sql = "SELECT * from (SELECT room.roomcode,room.`status`,room.typeid,ruzhu.`name`,ruzhu.tel,ruzhu.idcard,type.tname,t_order.rzdate,t_order.leavedate,t_order.price,ruzhu.id,t_tuifang.outdate,ruzhu.`status` as a,t_xvfangjilu.endtime \n" +
                "                FROM t_room room LEFT JOIN t_ruzhudetail ruzhu ON room.roomcode = ruzhu.roomid\n" +
                "                LEFT JOIN t_type type ON type.id = room.typeid \n" +
                "                LEFT JOIN t_order ON t_order.id = ruzhu.orderid \n" +
                "                LEFT JOIN t_tuifang ON t_tuifang.ruzhuid=ruzhu.id\n" +
                "                LEFT JOIN t_xvfangjilu ON ruzhu.id= t_xvfangjilu.ruzhuid " +
                "order by room.roomcode,ruzhu.id desc LIMIT 1000) aaa GROUP BY roomcode;";
        List<Map> maps = BaseDB.queryMap(sql);
//        for (Map map : maps) {
//            if (map.get("a")==null || Integer.parseInt(map.get("a")+"")==1){
//
//                arrayList.add(map);
//            }
//
//
//        }
        return maps;
    }

    @Override
    public List<Map> tuifang(Integer rzxq) {
        /**
         * 获取到的有房间号，订单的id，入住的押金，价格，订单的状态，入住的时间和房到点的时间
         */
        String sql ="SELECT rz.roomid,rz.orderid,rz.rzyj,dd.price,dd.`status`,dd.rzdate,dd.leavedate from t_ruzhudetail rz LEFT JOIN t_order dd ON dd.id=rz.orderid where rz.id=?";
        return JDBCUntil.selectMap(sql,rzxq);
    }

    @Override
    public Double typeroomprice(Integer rzxq) {
        String sql ="select t_type.money from t_ruzhudetail left join t_order on t_ruzhudetail.orderid=t_order.id JOIN t_type ON t_type.id=t_order.roomtype where t_ruzhudetail.id=?";
        List<Map> maps = JDBCUntil.selectMap(sql, rzxq);
        System.out.println(maps);
        Object money = maps.get(0).get("money");
        System.out.println(money);
        Double i =(Double) money;
        return i;
    }

    /**+
     * 修改提前退房的顶订单状态
     * @param orderid
     * @return
     */
    @Override
    public int setstuid(Integer orderid) {
        String sql="update t_order set status=3 where id=?";
        return JDBCUntil.Updateout(sql,orderid);
    }
    /**+
     * 修改正常退房的订单状态
     * @param orderid
     * @return
     */
    @Override
    public int setstuids(Integer orderid) {
        String sql="update t_order set status=4 where id=?";
        return JDBCUntil.Updateout(sql,orderid);
    }
    /**+
     * 修改正常退房的房间状态
     * @param roomcode
     * @return
     */
    @Override
    public int fangjianstu(Integer roomcode) {
        String sql="update t_room set status=1 where roomcode=?";
        return JDBCUntil.Updateout(sql,roomcode);
    }
    /**+
     * 打扫房间
     * @param roomcode
     * @return
     */
    @Override
    public int dasao(Integer roomcode) {
        String sql="update t_room set status=2 where roomcode=?";
        return JDBCUntil.Updateout(sql,roomcode);
    }

    @Override
    public int addtuizhu(TuiFang tuiFang) {
        String sql ="insert into t_tuifang (ruzhuid,tszjr,orderid,outdate,tsmoney) values(?,'前台',?,now(),?)";
        return JDBCUntil.Updateout(sql,tuiFang.getRuzhuid(),tuiFang.getOrderid(),tuiFang.getFantui());

    }

//    @Override
//    public int updruzhustatus(Integer ruzhuid) {
//        String  sql = "update t_ruzhudetail set status=0 where id=?";
//        return JDBCUntil.Updateout(sql,ruzhuid);
//    }

    @Override
    public List<Map> selectsy(Integer ruzhuid) {
        String sql="select t_room.typeid from t_ruzhudetail left join t_room on t_ruzhudetail.roomid=t_room.roomcode where t_ruzhudetail.id=? ";
        return JDBCUntil.selectMap(sql,ruzhuid);
    }

    @Override
    public List<Map> selectzs(int typeid) {
        String sql="select * from t_room left join t_type on t_room.typeid=t_type.id where t_room.typeid=? and t_room.status=2";
        return JDBCUntil.selectMap(sql,typeid);
    }

    @Override
    public List<Map> selectqdhf(Integer rzid) {
        String sql="select * from t_ruzhudetail LEFT JOIN t_order on t_ruzhudetail.orderid=t_order.id where t_ruzhudetail.id=? and t_ruzhudetail.status=1";

        return JDBCUntil.selectMap(sql,rzid);
    }

    @Override
    public int updroom(Integer roomid,Integer rmcode) {
        String sql="update t_room set status=1 where roomcode=?";
        int updateout = JDBCUntil.Updateout(sql, roomid);

        return updateout;
    }

    @Override
    public int updxq(Integer rzid, Integer rmcode) {
        String sql="update t_ruzhudetail set roomid=? where id=?";
        return JDBCUntil.Updateout(sql,rmcode,rzid);
    }

    @Override
    public int updroom1(int roomid, Integer rmcode) {
        String sql1="update t_room set status=0 where roomcode=?";
        int updateout1 = JDBCUntil.Updateout(sql1, rmcode);
        return updateout1;
    }

    @Override
    public int huangfangjilu(int roomid, Integer rmcode, Integer rzid) {
        String  sql = "insert into t_huanfangjilu (ruzhuid,time,oldroomid,newroomid) values (?,now(),?,?)";
        return JDBCUntil.Updateout(sql,rzid,roomid,rmcode);

    }

    @Override
    public Date getEndtime(Integer ruzhuid) {
        String sql ="select t_order.leavedate from t_ruzhudetail join t_order on t_order.id=t_ruzhudetail.orderid where t_ruzhudetail.id=? ";

        List<Map> maps = JDBCUntil.selectMap(sql, ruzhuid);
        Object leavedate1 = maps.get(0).get("leavedate");
        Date leavedate = (Date)leavedate1;
        return leavedate;
    }

    @Override
    public List<Map> selxufangjilu(Integer ruzhuid) {
        String sql ="select * from t_xvfangjilu where ruzhuid=?";
        return JDBCUntil.selectMap(sql,ruzhuid);
    }

    @Override
    public List<Map> selroomtype(Integer ruzhuid) {
        String sql ="select t_room.typeid from t_ruzhudetail join t_room on t_ruzhudetail.roomid=t_room.roomcode where t_ruzhudetail.id=?";
        return JDBCUntil.selectMap(sql,ruzhuid);
    }

    @Override
    public int insertxufang(Integer ruzhuid,String begin,String endtime,Double xufangjiage) {
        String sql ="insert into t_xvfangjilu (ruzhuid,begintime,endtime,money) values (?,?,?,?)";
        return JDBCUntil.Updateout(sql,ruzhuid,begin,endtime,xufangjiage);
    }

}
